Authors- Rajat Malik (Cloud Engineer), Anubhav Bisht (Associate Cloud Engineer)
Overview
This walkthrough explains how to deploy a simple Azure Spring Cloud microservice application to run on Azure. If you want to know more about Azure Spring Cloud, you can read here: http://ifi.tech/2020/09/15/introduction-to-azure-spring-cloud/
The application code used in this tutorial is a simple app built with Spring Initializr. When you’ve completed this example, the application will be accessible online and can be managed via the Azure portal.
This quickstart explains how to:
- Generate a basic Spring Cloud project
- Provision a service instance
- Build and deploy the app with a public endpoint
Prerequisites
Before we start with the quickstart, we will need the following:
- Install JDK 8
- Sign up for an Azure subscription (Sign up for free)
- Install Maven
- Install the Azure CLI version 2.0.67 or higher and the Azure Spring Cloud extension with command:
az extension add –name spring-cloud
Generate a Spring Cloud project
Start with Spring Initializr to generate a sample project with recommended dependencies for Azure Spring Cloud. The following image shows the Initializr set up for this sample project.
Click to open the Spring Initializr
Once the Artifact name and other dependencies are set, Click Generate. Download the zip file and and extract the package to a folder.
The next step is to create a HelloController.java file (web controller for a simple web application) by adding a new file in the src/main/java/com/example/helloIFI/ folder as follows (same can be done using a code editor):
Set up an instance of Azure Spring Cloud
The following procedure creates an instance of Azure Spring Cloud using the Azure portal. For this, open your browser and follow the following steps:
- In a new tab, open the Azure portal.
- On the top left of the portal, click on the hamburger button(≡), click on Create a resource, search for Azure Spring Cloud.
- Select Azure Spring Cloud from the results as shown in the image:
Fill out the form on the Azure Spring Cloud Create page. Consider the following guidelines:
- Subscription: Select the subscription you want to be billed for this resource.
- Resource group: Creating new resource groups for new resources is a best practice. This will be used in later steps as <resource group name>.
- Service Details/Name: Specify the <service instance name>. The name must be between 4 and 32 characters long and can contain only lowercase letters, numbers, and hyphens. The first character of the service name must be a letter and the last character must be either a letter or a number.
- Location: Select the region for your service instance.
Make sure that you use globally unique name while spinning up the Azure spring cloud.
Once you fill the Subscription, Resource Group, unique name, Region and select the Pricing Tier, click on Review & create.
Build and Deploy Process
- On your local machine, open a command prompt by typing cmd on the address bar of the folder where you have extracted the helloIFI folder (downloaded and extracted from Spring Initializr above). We are going to build this project using Maven by typing the following command in the command prompt
mvn clean package -DskipTests
Once the build is complete, a HelloIFI-0.0.1-SNAPSHOT.Jar file is created in the \target folder, which you can check by going to the C:/Users/<username>/HelloIFI/target folder.
2. You can install the Azure Spring Cloud extension for the Azure CLI (You might need to use azlogin if you are logging in from Azure CLI for the first time, to connect to your Azure account) by running the following command on the command prompt:
az extension add –name spring-cloud
3. To create the app with public endpoint assigned, run the following command:
az spring-cloud app create -n helloIFI -s <service instance name> -g <resource group name> –is-public
4. The next step is to deploy the Jar file for the app using the following command:
az spring-cloud app deploy -n helloIFI -s <service instance name> -g <resource group name> –jar-path target\helloIFI-0.0.1-SNAPSHOT.jar
5. It takes a few minutes to finish deploying the application. To confirm that it has deployed, go to the Apps blade in the Azure portal. You should see the status of the application.
Click on the Test Endpoint link to get the actual output of the application in the web.
So, this walk through showed how you can deploy a Spring based application to Azure Spring Cloud for hosting and monitoring purpose. There are many other features like Networking, Scaling, etc. Depending upon the requirement of the project and the application you can configure them accordingly.